/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package com.feng.gaoerfu.base;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.feng.gaoerfu.R;
import com.feng.gaoerfu.bean.BallPersonData;
import com.feng.gaoerfu.utils.SharedPreferencesHelper;
import com.feng.gaoerfu.view.StatusBarCompat;

import java.util.ArrayList;

import butterknife.ButterKnife;

/**
 * @author zeyu.jia
 * @version $Rev$
 * @time 2016/5/27 0027 13:18
 * @des 这是基于MVC框架的activity基类
 * @updateAuthor $Author$
 * @updateDate $Date$
 * @updateDes ${TODO}
 */
public abstract class BaseActivity extends FragmentActivity {
    private static final String TAG = "BaseActivity";

    /**
     * 在onCreate方法中赋值，不能在子Activity中创建
     */
    protected BaseActivity mContext = null;

    /**
     * 进度弹窗
     */
    protected ProgressDialog progressDialog = null;

    public boolean isSetStatuBar = true;
    public SharedPreferencesHelper mSpHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        mSpHelper = App.mSpHelper;
        setContentView(initContentView());
        ButterKnife.inject(this);
        initView();
        StatusBarCompat.setColorNoTranslucent(this, getResources().getColor(R.color.text_black));
        initData();
        initListener();

    }


    /**
     * 设置contentView的方法必须实现
     *
     * @return contentView
     */
    public abstract int initContentView();

    /**
     * UI显示方法，必须在子类onCreate方法内setContentView后调用
     */
    public abstract void initView();

    /**
     * data数据方法，必须在子类onCreate方法内setContentView后调用
     */
    public abstract void initData();

    /**
     * listener事件监听方法，必须在子类onCreate方法内setContentView后调用
     */
    public void initListener() {
    }


    //显示与关闭进度弹窗方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 展示加载进度条,无标题
     *
     * @param stringResId
     */
    public void showProgressDialog(int stringResId) {
        try {
            showProgressDialog(null, mContext.getResources().getString(stringResId));
        } catch (Exception e) {
            Log.e(TAG, "showProgressDialog  showProgressDialog(null, mContext.getResources().getString(stringResId));");
        }
    }

    /**
     * 展示加载进度条,无标题
     *
     * @param dialogMessage
     */
    public void showProgressDialog(String dialogMessage) {
        showProgressDialog(null, dialogMessage);
    }

    /**
     * 展示加载进度条
     *
     * @param dialogTitle   标题
     * @param dialogMessage 信息
     */
    public void showProgressDialog(final String dialogTitle, final String dialogMessage) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                if (progressDialog == null) {
                    progressDialog = new ProgressDialog(mContext);
                }
                if (progressDialog.isShowing() == true) {
                    progressDialog.dismiss();
                }
                if (dialogTitle != null && !"".equals(dialogTitle.trim())) {
                    progressDialog.setTitle(dialogTitle);
                }
                if (dialogMessage != null && !"".equals(dialogMessage.trim())) {
                    progressDialog.setMessage(dialogMessage);
                }
                progressDialog.setCanceledOnTouchOutside(false);
                progressDialog.show();
            }
        });
    }

    /**
     * 隐藏加载进度
     */
    public void dismissProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing() == true) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    progressDialog.dismiss();
                }
            });
        }
    }
    //显示与关闭进度弹窗方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //启动新Activity方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 打开新的Activity，向左滑入效果
     *
     * @param intent
     */
    public void toActivity(final Intent intent) {
        toActivity(intent, true);
    }
    /*public void toActivity(final Intent intent,final boolean isFinish) {
        toActivity(intent, -1, true);
	}*/

    /**
     * 打开新的Activity
     *
     * @param intent
     * @param showAnimation
     */
    public void toActivity(final Intent intent, final boolean showAnimation) {
        toActivity(intent, -1, showAnimation);
    }

    /**
     * 打开新的Activity，向左滑入效果
     *
     * @param intent
     * @param requestCode
     */
    public void toActivity(final Intent intent, final int requestCode) {
        toActivity(intent, requestCode, true);
    }

    /**
     * 打开新的Activity
     *
     * @param intent
     * @param requestCode
     * @param showAnimation
     */
    public void toActivity(final Intent intent, final int requestCode, final boolean showAnimation) {
        if (intent == null) {
            Log.e(TAG, "toActivity  isAlive == false || intent == null >> return;");
            return;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                if (requestCode < 0) {
                    startActivity(intent);
                } else {
                    startActivityForResult(intent, requestCode);
                }

            }
        });

    }
    //启动新Activity方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //show short toast 方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 快捷显示short toast方法，需要long toast就用 Toast.makeText(string, Toast.LENGTH_LONG).show(); ---不常用所以这个类里不写
     *
     * @param stringResId
     */
    public void showShortToast(int stringResId) {
        try {
            showShortToast(mContext.getResources().getString(stringResId));
        } catch (Exception e) {
            Log.e(TAG, "showShortToast  mContext.getResources().getString(resId) >>  catch (Exception e) {" + e.getMessage());
        }
    }

    /**
     * 快捷显示short toast方法，需要long toast就用 Toast.makeText(string, Toast.LENGTH_LONG).show(); ---不常用所以这个类里不写
     *
     * @param string
     */
    public void showShortToast(final String string) {
        showShortToast(string, false);
    }

    /**
     * 快捷显示short toast方法，需要long toast就用 Toast.makeText(string, Toast.LENGTH_LONG).show(); ---不常用所以这个类里不写
     *
     * @param string
     * @param isForceDismissProgressDialog
     */
    public void showShortToast(final String string, final boolean isForceDismissProgressDialog) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (isForceDismissProgressDialog) {
                    dismissProgressDialog();
                }
                Toast.makeText(mContext, "" + string, Toast.LENGTH_SHORT).show();
            }
        });
    }
    //show short toast 方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    @Override
    public void finish() {
        super.finish();//必须写在最前才能显示自定义动画
        //        LogUtils.i(getClass().getSimpleName(), "onFinish");
    }

    @Override
    protected void onResume() {
        super.onResume();
        //        LogUtils.i(getClass().getSimpleName(), "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        //        LogUtils.i(getClass().getSimpleName(), "onPause");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //        LogUtils.i(getClass().getSimpleName(), "onDestroy");
        //里面的代码不需要重写，通过super.onDestroy();即可得到<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        dismissProgressDialog();
    }

    public void storeListToSp(ArrayList<BallPersonData> list) {
        String jsonString = JSONArray.toJSONString(list);
        Log.d(TAG, "jsonString = " + jsonString);
        mSpHelper.putStringValue("friend", jsonString);
    }

    public boolean hasSamePerson(BallPersonData personData) {
        return hasSamePerson(personData.name);
    }
    public boolean hasSamePerson(String name) {
        String stringValue = mSpHelper.getStringValue(Constants.SP_KEY_FRIEND);
        if (!TextUtils.isEmpty(stringValue)) {
            ArrayList<BallPersonData> list = (ArrayList<BallPersonData>) JSONArray.parseArray(stringValue, BallPersonData.class);
            for (int i = 0; i < list.size(); i++) {
                BallPersonData ballPersonData = list.get(i);
                if (ballPersonData.name.equals(name)) {
                    return true;
                }
            }
            return false;
        } else {
            return false;
        }
    }


}